
package bean.reflection.indexed;

import java.beans.IndexedPropertyDescriptor;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * You have to study TestInvokeReadMethod.java<br>
 * Test invoking write method of indexed property<br>
 * <br>
 * Copyright: Copyright (c) 2001-2007<br>
 * Company: 一零四資訊科技股份有限公司
 * 
 * @author Eric.Kao(eric.kao@104.com.tw)
 * @version 1.0
 */
public class TestInvokeWriteMethod
{
	public TestInvokeWriteMethod()
		throws Exception
	{
		ComplexBean bean = new ComplexBean();

		PropertyDescriptor[] descriptors = Introspector.getBeanInfo( bean.getClass() ).getPropertyDescriptors();

		for( int i = 0; i < descriptors.length; i++ )
		{
			PropertyDescriptor descriptor = descriptors[ i ];

			if( descriptor instanceof IndexedPropertyDescriptor && descriptor.getName().equals( "name" ) )
			{
				IndexedPropertyDescriptor ipd = ( IndexedPropertyDescriptor )descriptor;

				Method ipdWriteMethod = ipd.getIndexedWriteMethod();

				// 啟動 method, 並傳入參數
				Object[] args =
					{ new Integer( 1 ), "test" };

				Object returnValue = ipdWriteMethod.invoke( bean, args );

				System.out.println( "Return Object:" + returnValue );
			}
		}

		// print the value of java bean
		String[] strs = bean.getNames();
		for( int i = 0; i < strs.length; i++ )
		{
			System.out.println( "Name:" + strs[ i ] );
		}
	}

	public static void main( String[] args )
	{
		try
		{
			new TestInvokeWriteMethod();
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
		}
	}
}
